package com.example.yuejian23.yuejian_23.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yuejian23.yuejian_23.Data.Mdata;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.LoginBean;
import com.example.yuejian23.yuejian_23.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by dong on 2017/9/25.
 */

public class LoginActivity extends AppCompatActivity {
    private EditText username;
    private EditText userpass;
    private EditText et_username;
    private Button login;
    private Button reg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initEvents();


    }



    private void initView() {
        View v = findViewById(R.id.loginbg);
        v.getBackground().setAlpha(100);

        username = (EditText) findViewById(R.id.editText);
        userpass = (EditText) findViewById(R.id.editText2);
        login = (Button) findViewById(R.id.button);
        reg = (Button)findViewById(R.id.button2);
        et_username = (EditText)findViewById(R.id.editText);

    }

    public void initEvents(){
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                UserModel userModel=new UserModel();
                Call call=userModel.login(username.getText().toString(),userpass.getText().toString());
                Callback<LoginBean> callback = new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        //String userid = String.valueOf(response.body().getCode());
                        int userid = response.body().getCode();
                        if(userid == 0)
                        {
                            Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                            //失败处理
                        }else
                        {
                            Mdata.uid = String.valueOf(userid);
                            //Toast.makeText(LoginActivity.this,Mdata.uid,Toast.LENGTH_LONG).show();
                            Intent intent=new Intent();
                            intent.setClass(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                            saveUser(String.valueOf(userid));
                            //成功处理
                        }
                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {

                    }
                };
                call.enqueue(callback);
            }
        });

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegActivity.class);
                startActivityForResult(intent,1);

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==RESULT_OK) {
            String username = data.getStringExtra("num");
            et_username.setText(username);
        }
    }
    public  void  saveUser(String user_id){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username",username.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putString("user_id", String.valueOf(userid));
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();//提交修改
    }
}
